package com.vue;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.metier.*;
import com.modele.ModeleBornes;

import java.util.*;

import com.persistance.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Classe Jpanel d'ajout de Borne qui sera integre au Jform principale
 * 
 * @author b0dian
 * @version 1.0
 *
 */
public class ListeTypeCharges extends JPanel {
	private JLabel lblTypeDeCharge;
	private JComboBox<String> cbxTypeCharge;
	private JScrollPane scrollPane;
	private JTable table;
	private List<TypeCharge> type;
	private ModeleBornes modele;

	/**
	 * Create the panel.
	 */
	public ListeTypeCharges() {
		setLayout(null);

		add(getLblTypeDeCharge());
		add(getScrollPane());
		add(getCbxTypeCharge());

	}

	/**
	 * Create label
	 */
	private JLabel getLblTypeDeCharge() {
		if (lblTypeDeCharge == null) {
			lblTypeDeCharge = new JLabel("Type de charge");
			lblTypeDeCharge.setBounds(37, 58, 96, 14);
			lblTypeDeCharge.setFont(new Font("Tahoma", Font.BOLD, 11));
		}
		return lblTypeDeCharge;
	}

	/**
	 * Create ComboBox Et rempli cette comboxBox avec les libelle de TypeCharge
	 * recuperes grace au accesData Il y a egalement un evenement qui modifiera
	 * le contenu de la table en fonction du choix effectue sur la comboBox
	 */
	private JComboBox<String> getCbxTypeCharge() {
		if (cbxTypeCharge == null) {
			cbxTypeCharge = new JComboBox<String>();
			cbxTypeCharge.setBounds(193, 55, 132, 20);
			type = new ArrayList<TypeCharge>();
			type = AccesData.getTypesCharges();
			for (TypeCharge t : type) {
				cbxTypeCharge.addItem(t.getLibelleTypeCharge());
			}
			cbxTypeCharge.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (cbxTypeCharge.getSelectedIndex() != -1) {
						modele = new ModeleBornes(type.get(
								cbxTypeCharge.getSelectedIndex())
								.getCodeTypeCharge());
						table.setModel(modele);
						// Raffraichir le Modele
						table.revalidate();
					}
				}
			});
			cbxTypeCharge.setSelectedIndex(0);
		}
		return cbxTypeCharge;
	}

	/**
	 * Create scrollPane Auquel nous integrerons le Jtable
	 */
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 89, 430, 200);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	/**
	 * Create Jtable En lui passant en parramètre le modele correspondant
	 */
	private JTable getTable() {
		if (table == null) {
			table = new JTable(modele);
			table = new JTable();
		}
		return table;
	}
}
